package com.runmyai.aitools.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;

/**
 * @author wry
 */
public class SensitiveWordsFilter {

    public static final Set<String> SENSITIVE_COLLECTION = new HashSet<>();
    public static final String SENSITIVE_REGEX;

    static {
        try (InputStream inputStream = SensitiveWordsFilter.class.getClassLoader().getResourceAsStream("sensitive-words.txt")) {
            if (inputStream != null) {
                try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
                    reader.lines().forEach(SENSITIVE_COLLECTION::add);
                } catch (IOException e) {

                    throw new RuntimeException("Error reading from input stream", e);
                }
            }
            SENSITIVE_REGEX = String.join("|", SENSITIVE_COLLECTION);
        } catch (IOException e) {
            throw new RuntimeException("Failed to load sensitive words from file", e);
        }
    }

    public static String filter(String input) {
        return input.replaceAll(SENSITIVE_REGEX, "");
    }
}
